<template>
  <PageWrapper contentFullHeight fixedHeight contentClass="main-box">
    <CommonDateSearch ref="searchRef" @search="handleSearch"></CommonDateSearch>
    <a-card :class="['common-card flex', typeList.length > 4 && 'list-more']" title="分系统化学品费用">
      <div class="chart-box" v-for="item in typeList" :key="item.dictCode">
        <BarLine class="chart" v-if="item.type == 0" :loading="item.loading" :chartData="item.chartData"
          :title="item.itemName" />
        <Line class="chart" v-else :loading="item.loading" :chartData="item.chartData" :title="item.itemName" :unit="item.chartData.yaxisList[0]?.unit || '元/m³'" />

        <!-- <BarLine class="chart" :loading="item.loading" :chartData="item.chartData" :title="item.itemName" /> -->
      </div>
    </a-card>
  </PageWrapper>
</template>
<script setup lang="ts">
import BarLine from '@/components/Charts/BarLine/index.vue'
import Line from '@/components/Charts/Line/index.vue'
import { ref, onMounted, reactive } from 'vue'
import { PageWrapper } from '@/components/Page'
// import CommonDateSearch from '@/components/CommonDateSearch/index.vue'
import CommonDateSearch from '../../CommonDateSearch.vue'
import { getTypeUsageCostChartData, getChildDataCostDiv } from '@/api/common/chart'
import { getDictListByCode } from '@/api/sys/dict'
import { CommonTypeList } from '@/api/common/model/chart'

defineOptions({ name: 'OutletWaterElectricityConsume' })

const searchRef = ref()
const searchInfo = reactive({})
const typeList = ref<CommonTypeList[]>([])

const dictCode = 'water_wastewater_chemical_cost_of_system'

onMounted(async () => {
  await getTypeList()
  searchRef.value.handleSearch()
})
const handleSearch = (params) => {
  Object.assign(searchInfo, params)
  getChartData()
}
const getTypeList = async () => {
  const res = await getDictListByCode(dictCode)
  typeList.value = res.map((item) => ({
    ...item,
    loading: true,
    chartData: {
      xaxisList: [],
      yaxisList: [],
    },
  }))
}
const getChartData = () => {
  typeList.value.forEach((item) => {
    console.log(item);
    if(item.itemName === '吨水单价') {
      item.type = 1;
          getChildDataCostDiv({
          ...searchInfo,
          dictCode,
          itemName: item.itemName,
        }).then((res) => {

          item.chartData = res

        })
          .finally(() => {
            item.loading = false
          })
    } else {
      item.type = 0;
      getTypeUsageCostChartData({
        ...searchInfo,
        dictCode,
        itemName: item.itemName,
        // classifyId: item.itemCode
      })
        .then((res) => {
          item.chartData = res
        })
        .finally(() => {
          item.loading = false
        })
    }
    item.loading = true
  })
}
</script>
<style scoped lang="scss">
.common-card {
  flex: 1;
  overflow: hidden;

  &.list-more {
    :deep(.ant-card-body) {
      overflow: auto !important;
    }

    .chart-box {
      height: 45% !important;
    }
  }

  :deep(.ant-card-body) {
    display: flex;
    flex: 1;
    flex-wrap: wrap;
    padding: 4px;
    overflow: hidden;

    .chart-box {
      flex-shrink: 0;
      width: 50%;
      height: 100%;
      min-height: 240px;
      padding: 4px;
    }
  }
}
</style>